package com.sgb.kjwl.view.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.user.TwoOrThreeLevelEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.entity.MenuEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author qiangHeNoooo
 * 说明 ： 商品的三级分类筛选控件
 * 时间 ： 2021.06.30
 */
public class GoodsThreeLevelCategorizeView extends LinearLayout {

    private int indexCid1 = 0;
    private int indexCid2 = 0;
    private final TabLayout tlFragmentGoodsMainGoodsSelectTitle1;
    private final TabLayout tlFragmentGoodsMainGoodsSelectTitle2;
    private final TabLayout tlFragmentGoodsMainGoodsSelectTitle3;
    private final ImageView ivFragmentGoodsMainGoodsTag1;
    private final ImageView ivFragmentGoodsMainGoodsTag2;
    private final ImageView ivFragmentGoodsMainGoodsTag3;
    private final ImageView ivGoodsListSearch;
    private TabItemSelectedResultListener tabItemSelectedResultListener;

    private List<MenuEntity<MenuEntity<MenuEntity>>> menuEntityList;
    /**
     * 一级选中分类实体
     */
    private MenuEntity<MenuEntity<MenuEntity>> firstMenuEntity;
    /**
     * 二级选中分类实体
     */
    private MenuEntity<MenuEntity> secondMenuEntity;
    /**
     * 三级选中分类实体
     */
    private MenuEntity thirdMenuEntity;
    private boolean isHaveSearch;
    private final OnTabSelectedListener1 onTabSelectedListener1 = new OnTabSelectedListener1();
    private final OnTabSelectedListener2 onTabSelectedListener2 = new OnTabSelectedListener2();
    private final OnTabSelectedListener3 onTabSelectedListener3 = new OnTabSelectedListener3();


    /**
     * 行业匹配的一级分类列表
     */
    private List<MenuEntity> firstMenuEntity_n = new ArrayList<>();
    /**
     * 行业匹配的二级分类列表
     */
    private List<MenuEntity<MenuEntity>> secondMenuEntity_n = new ArrayList<>();
    /**
     * 行业匹配的三级分类列表
     */
    private List<MenuEntity> thirdMenuEntity_n = new ArrayList<>();

    private String mAreaCode = "";

    private boolean usenew = false;


    private String secondCategory;
    private String thirdCategory;

    public GoodsThreeLevelCategorizeView(Context context) {
        this(context, null);

    }

    @SuppressLint("InflateParams")
    public GoodsThreeLevelCategorizeView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        this(context, attrs, 0);

    }

    @SuppressLint("InflateParams")
    public GoodsThreeLevelCategorizeView(Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.view_goods_three_level, this);

        tlFragmentGoodsMainGoodsSelectTitle1 = findViewById(R.id.tl_fragment_goods_main_goods_select_title1);
        tlFragmentGoodsMainGoodsSelectTitle2 = findViewById(R.id.tl_fragment_goods_main_goods_select_title2);
        tlFragmentGoodsMainGoodsSelectTitle3 = findViewById(R.id.tl_fragment_goods_main_goods_select_title3);

        ivFragmentGoodsMainGoodsTag1 = findViewById(R.id.iv_fragment_goods_main_goods_tag1);
        ivFragmentGoodsMainGoodsTag2 = findViewById(R.id.iv_fragment_goods_main_goods_tag2);
        ivFragmentGoodsMainGoodsTag3 = findViewById(R.id.iv_fragment_goods_main_goods_tag3);
        ivGoodsListSearch = findViewById(R.id.iv_goods_list_search);

        tlFragmentGoodsMainGoodsSelectTitle1.addOnTabSelectedListener(onTabSelectedListener1);
        tlFragmentGoodsMainGoodsSelectTitle2.addOnTabSelectedListener(onTabSelectedListener2);
        tlFragmentGoodsMainGoodsSelectTitle3.addOnTabSelectedListener(onTabSelectedListener3);
    }

    final class OnTabSelectedListener1 implements TabLayout.OnTabSelectedListener {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            indexCid1 = tab.getPosition();
            secondMenuEntity = null;
            thirdMenuEntity = null;
            if (usenew) {
                firstMenuEntity = firstMenuEntity_n.get(indexCid1);
                getClassListeData(firstMenuEntity.getId(), true);
            } else {
                firstMenuEntity = menuEntityList.get(indexCid1);
                refreshTabTitle(indexCid1, 0);
                if (tabItemSelectedResultListener != null) {
                    tabItemSelectedResultListener.onFirstCategorizeCallBack(firstMenuEntity, indexCid1);
                }
            }
            if (indexCid1 == 0) {
                tlFragmentGoodsMainGoodsSelectTitle2.setVisibility(View.GONE);
                ivFragmentGoodsMainGoodsTag2.setVisibility(View.GONE);
            } else {
                tlFragmentGoodsMainGoodsSelectTitle2.setVisibility(View.VISIBLE);
                ivFragmentGoodsMainGoodsTag2.setVisibility(View.VISIBLE);
            }
            tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.GONE);
            ivFragmentGoodsMainGoodsTag3.setVisibility(View.GONE);

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    }

    final class OnTabSelectedListener2 implements TabLayout.OnTabSelectedListener {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            indexCid2 = tab.getPosition();
            thirdMenuEntity = null;
            if (usenew) {
                secondMenuEntity = secondMenuEntity_n.get(indexCid2);
                getClassListeData(secondMenuEntity.getId(), false);
            } else {
                if (menuEntityList.get(indexCid1).getDatas() != null && menuEntityList.get(indexCid1).getDatas().size() != 0) {
                    secondMenuEntity = menuEntityList.get(indexCid1).getDatas().get(indexCid2);
                } else {
                    secondMenuEntity = null;
                }
                refreshTabTitle(indexCid1, indexCid2);
                if (tabItemSelectedResultListener != null) {
                    tabItemSelectedResultListener.onSecondCategorizeCallBack(secondMenuEntity, indexCid2);
                }
            }
            if (indexCid2 == 0) {
                tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.GONE);
                ivFragmentGoodsMainGoodsTag3.setVisibility(View.GONE);
            } else {
                tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.VISIBLE);
                ivFragmentGoodsMainGoodsTag3.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    }

    final class OnTabSelectedListener3 implements TabLayout.OnTabSelectedListener {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            if (usenew) {
                if (tab.getPosition()!=0) {
                    thirdMenuEntity = thirdMenuEntity_n.get(tab.getPosition());
                } else {
                    thirdMenuEntity = null;
                }

            } else {
                if (menuEntityList.get(indexCid1).getDatas().get(indexCid2) != null && menuEntityList.get(indexCid1).getDatas().get(indexCid2).getDatas() != null
                        && menuEntityList.get(indexCid1).getDatas().get(indexCid2).getDatas().size() != 0) {
                    thirdMenuEntity = menuEntityList.get(indexCid1).getDatas().get(indexCid2).getDatas().get(tab.getPosition());
                } else {
                    thirdMenuEntity = null;
                }

            }
            if (tabItemSelectedResultListener != null) {
                tabItemSelectedResultListener.onThirdCategorizeCallBack(thirdMenuEntity, tab.getPosition());
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    }

    /**
     * 设置二三级数据
     *
     * @param indexCid1 一级筛选下标
     * @param indexCid2 二级筛选下标
     */
    private void refreshTabTitle(int indexCid1, int indexCid2) {
        if (indexCid1 == 0) {
            tlFragmentGoodsMainGoodsSelectTitle2.removeAllTabs();
            tlFragmentGoodsMainGoodsSelectTitle3.removeAllTabs();
        } else if (!usenew) {
            tlFragmentGoodsMainGoodsSelectTitle2.removeAllTabs();
            List<MenuEntity<MenuEntity>> categoriesGoodsEntity = menuEntityList.get(indexCid1).getDatas();
            for (MenuEntity children2 : categoriesGoodsEntity) {
                tlFragmentGoodsMainGoodsSelectTitle2.addTab(tlFragmentGoodsMainGoodsSelectTitle2.newTab().setText(children2.getName()), false);
            }
            tlFragmentGoodsMainGoodsSelectTitle2.removeOnTabSelectedListener(onTabSelectedListener2);
            tlFragmentGoodsMainGoodsSelectTitle2.postDelayed(() -> {
                if (tlFragmentGoodsMainGoodsSelectTitle2.getTabCount() > indexCid2 && tlFragmentGoodsMainGoodsSelectTitle2.getTabAt(indexCid2) != null) {
                    tlFragmentGoodsMainGoodsSelectTitle2.getTabAt(indexCid2).select();
                }
                tlFragmentGoodsMainGoodsSelectTitle2.addOnTabSelectedListener(onTabSelectedListener2);
            }, 100);
            if (indexCid2 == 0) {
                tlFragmentGoodsMainGoodsSelectTitle3.removeAllTabs();
            } else {
                tlFragmentGoodsMainGoodsSelectTitle3.removeAllTabs();
                List<MenuEntity> menuEntities = menuEntityList.get(indexCid1).getDatas().get(indexCid2).getDatas();
                for (MenuEntity children2 : menuEntities) {
                    tlFragmentGoodsMainGoodsSelectTitle3.addTab(tlFragmentGoodsMainGoodsSelectTitle3.newTab().setText(children2.getName()), false);
                }
                tlFragmentGoodsMainGoodsSelectTitle3.removeOnTabSelectedListener(onTabSelectedListener3);
                tlFragmentGoodsMainGoodsSelectTitle3.postDelayed(() -> {
                    if (tlFragmentGoodsMainGoodsSelectTitle3.getTabCount() > 0 && tlFragmentGoodsMainGoodsSelectTitle3.getTabAt(0) != null) {
                        tlFragmentGoodsMainGoodsSelectTitle3.getTabAt(0).select();
                    }
                    tlFragmentGoodsMainGoodsSelectTitle3.addOnTabSelectedListener(onTabSelectedListener3);
                }, 100);
            }


        }
    }

    /**
     * 加载分类数据
     *
     * @param menuEntities     三级分类筛选数据
     * @param isHaveSearch     是否显示收缩图标
     * @param firstCategoryId  第一级商品分类筛选id
     * @param secondCategoryId 第二级商品分类筛选id
     * @param thirdCategoryId  第三级商品分类筛选id
     */
    public void loadData(List<MenuEntity<MenuEntity<MenuEntity>>> menuEntities, boolean isHaveSearch, @Nullable String firstCategoryId, @Nullable String secondCategoryId, @Nullable String thirdCategoryId) {
        if (menuEntities == null || menuEntities.size() == 0) {
            return;
        }
        this.isHaveSearch = isHaveSearch;
        if (isHaveSearch) {
            this.ivGoodsListSearch.setVisibility(View.VISIBLE);
            this.ivGoodsListSearch.setOnClickListener(v -> {
                if (tabItemSelectedResultListener != null) {
                    tabItemSelectedResultListener.onSearchClickListener();
                }
            });
        } else {
            this.ivGoodsListSearch.setVisibility(View.GONE);
        }
        this.menuEntityList = menuEntities;
        setSpecifyCategory(firstCategoryId, secondCategoryId, thirdCategoryId);
    }


    public void loadDataNew(List<MenuEntity> listdata, boolean isHaveSearch, @Nullable String firstCategoryId, @Nullable String secondCategoryId, @Nullable String thirdCategoryId, String mAreaCode) {
        this.mAreaCode = mAreaCode;
        this.usenew = true;
        if (listdata == null && listdata.size() == 0) {
            this.setVisibility(GONE);
            return;
        } else {
            this.setVisibility(VISIBLE);
            this.isHaveSearch = isHaveSearch;
            if (isHaveSearch) {
                this.ivGoodsListSearch.setVisibility(View.VISIBLE);
                this.ivGoodsListSearch.setOnClickListener(v -> {
                    if (tabItemSelectedResultListener != null) {
                        tabItemSelectedResultListener.onSearchClickListener();
                    }
                });
            } else {
                this.ivGoodsListSearch.setVisibility(View.GONE);
            }

            this.firstMenuEntity_n = listdata;

            setSpecifyCategory(firstCategoryId, secondCategoryId, thirdCategoryId);
        }
    }

    /**
     * 跳转指定到目标分类
     *
     * @param firstCategory  第一级类目
     * @param secondCategory 第二级类目 为Null时不指定二级目录
     * @param thirdCategory  第三级类目  为Null时不指定三级目录
     */
    public void setSpecifyCategory(@Nullable String firstCategory, @Nullable String secondCategory, @Nullable String thirdCategory) {
        resetAllValue();
        if (usenew) {
            this.secondCategory = secondCategory;
            this.thirdCategory = thirdCategory;
            if (firstCategory == null) {
                tlFragmentGoodsMainGoodsSelectTitle1.setVisibility(GONE);
//                ivGoodsListSearch.setVisibility(GONE);
            } else {
                tlFragmentGoodsMainGoodsSelectTitle1.setVisibility(VISIBLE);
//                if(isHaveSearch) {
                    ivGoodsListSearch.setVisibility(VISIBLE);
//                }
            }
            for (int index1 = 0; index1 < firstMenuEntity_n.size(); index1++) {
                MenuEntity menuEntity = firstMenuEntity_n.get(index1);

                if (firstCategory != null && menuEntity.getId().equals(firstCategory)) {
                    this.firstMenuEntity = menuEntity;
                    this.indexCid1 = index1;
                    tlFragmentGoodsMainGoodsSelectTitle1.addTab(tlFragmentGoodsMainGoodsSelectTitle1.newTab().setText(menuEntity.getName()), true);
                }else{
                    tlFragmentGoodsMainGoodsSelectTitle1.addTab(tlFragmentGoodsMainGoodsSelectTitle1.newTab().setText(menuEntity.getName()), false);
                }
            }
            tlFragmentGoodsMainGoodsSelectTitle1.setVisibility(View.VISIBLE);
            ivFragmentGoodsMainGoodsTag1.setVisibility(View.VISIBLE);
            int finalIndexCid1 = indexCid1;

            tlFragmentGoodsMainGoodsSelectTitle1.removeOnTabSelectedListener(onTabSelectedListener1);
            tlFragmentGoodsMainGoodsSelectTitle1.postDelayed(() -> {
                if (tlFragmentGoodsMainGoodsSelectTitle1.getTabCount() > finalIndexCid1 && tlFragmentGoodsMainGoodsSelectTitle1.getTabAt(finalIndexCid1) != null) {
                    tlFragmentGoodsMainGoodsSelectTitle1.getTabAt(finalIndexCid1).select();
                }
                tlFragmentGoodsMainGoodsSelectTitle1.addOnTabSelectedListener(onTabSelectedListener1);
            }, 100);

        } else {
            if (menuEntityList == null || menuEntityList.size() == 0) {
                return;
            }
            if (firstCategory == null) {
                tlFragmentGoodsMainGoodsSelectTitle1.setVisibility(GONE);
                ivGoodsListSearch.setVisibility(GONE);
            } else {
                tlFragmentGoodsMainGoodsSelectTitle1.setVisibility(VISIBLE);
                ivGoodsListSearch.setVisibility(VISIBLE);
            }
            for (int index1 = 0; index1 < menuEntityList.size(); index1++) {
                MenuEntity<MenuEntity<MenuEntity>> menuEntity = menuEntityList.get(index1);
                tlFragmentGoodsMainGoodsSelectTitle1.addTab(tlFragmentGoodsMainGoodsSelectTitle1.newTab().setText(menuEntity.getName()), false);
                if (firstCategory != null && menuEntity.getId().equals(firstCategory)) {
                    this.firstMenuEntity = menuEntity;
                    this.indexCid1 = index1;
                }
            }
            tlFragmentGoodsMainGoodsSelectTitle1.setVisibility(View.VISIBLE);
            ivFragmentGoodsMainGoodsTag1.setVisibility(View.VISIBLE);
            int finalIndexCid1 = indexCid1;
            tlFragmentGoodsMainGoodsSelectTitle1.removeOnTabSelectedListener(onTabSelectedListener1);
            tlFragmentGoodsMainGoodsSelectTitle1.postDelayed(() -> {
                if (tlFragmentGoodsMainGoodsSelectTitle1.getTabCount() > finalIndexCid1 && tlFragmentGoodsMainGoodsSelectTitle1.getTabAt(finalIndexCid1) != null) {
                    tlFragmentGoodsMainGoodsSelectTitle1.getTabAt(finalIndexCid1).select();
                }
                tlFragmentGoodsMainGoodsSelectTitle1.addOnTabSelectedListener(onTabSelectedListener1);
            }, 100);
            if (this.firstMenuEntity != null) {
                if (tabItemSelectedResultListener != null) {
                    tabItemSelectedResultListener.onFirstCategorizeCallBack(firstMenuEntity, finalIndexCid1);
                }
            } else {
                if (tabItemSelectedResultListener != null) {
                    tabItemSelectedResultListener.onFirstCategorizeCallBack(menuEntityList.get(0), finalIndexCid1);
                }
                return;
            }

            List<MenuEntity<MenuEntity>> menuEntityList1 = menuEntityList.get(indexCid1).getDatas();
            if (secondCategory == null || menuEntityList1 == null || menuEntityList1.size() == 0) {
                return;
            }
            for (int index2 = 0; index2 < menuEntityList1.size(); index2++) {
                MenuEntity<MenuEntity> menuEntityMenuEntity = menuEntityList1.get(index2);
                tlFragmentGoodsMainGoodsSelectTitle2.addTab(tlFragmentGoodsMainGoodsSelectTitle2.newTab().setText(menuEntityMenuEntity.getName()), false);
                if (menuEntityMenuEntity.getId().equals(secondCategory)) {
                    this.secondMenuEntity = menuEntityMenuEntity;
                    this.indexCid2 = index2;
                }
            }
            if (this.secondMenuEntity != null) {
                tlFragmentGoodsMainGoodsSelectTitle2.setVisibility(View.VISIBLE);
                ivFragmentGoodsMainGoodsTag2.setVisibility(View.VISIBLE);
                int finalIndexCid2 = indexCid2;
                tlFragmentGoodsMainGoodsSelectTitle2.removeOnTabSelectedListener(onTabSelectedListener2);
                tlFragmentGoodsMainGoodsSelectTitle2.postDelayed(() -> {
                    if (tlFragmentGoodsMainGoodsSelectTitle2.getTabCount() > finalIndexCid2 && tlFragmentGoodsMainGoodsSelectTitle2.getTabAt(finalIndexCid2) != null) {
                        tlFragmentGoodsMainGoodsSelectTitle2.getTabAt(finalIndexCid2).select();
                    }
                    tlFragmentGoodsMainGoodsSelectTitle2.addOnTabSelectedListener(onTabSelectedListener2);
                }, 100);
                if (tabItemSelectedResultListener != null) {
                    tabItemSelectedResultListener.onSecondCategorizeCallBack(secondMenuEntity, finalIndexCid2);
                }
            } else {
                tlFragmentGoodsMainGoodsSelectTitle2.removeAllTabs();
                return;
            }
            List<MenuEntity> menuEntityList2 = menuEntityList.get(indexCid1).getDatas().get(indexCid2).getDatas();
            if (thirdCategory == null || menuEntityList2 == null || menuEntityList2.size() == 0) {
                return;
            }
            int indexCid3 = 0;
            for (int index3 = 0; index3 < menuEntityList2.size(); index3++) {
                MenuEntity menuEntity = menuEntityList2.get(index3);
                tlFragmentGoodsMainGoodsSelectTitle3.addTab(tlFragmentGoodsMainGoodsSelectTitle3.newTab().setText(menuEntity.getName()), false);
                if (menuEntity.getId().equals(thirdCategory)) {
                    this.thirdMenuEntity = menuEntity;
                    indexCid3 = index3;
                }
            }
            if (this.thirdMenuEntity != null) {
                tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.VISIBLE);
                ivFragmentGoodsMainGoodsTag3.setVisibility(View.VISIBLE);
                int finalIndexCid3 = indexCid3;
                tlFragmentGoodsMainGoodsSelectTitle3.removeOnTabSelectedListener(onTabSelectedListener3);
                tlFragmentGoodsMainGoodsSelectTitle3.postDelayed(() -> {
                    if (tlFragmentGoodsMainGoodsSelectTitle3.getTabCount() > finalIndexCid3 && tlFragmentGoodsMainGoodsSelectTitle3.getTabAt(finalIndexCid3) != null) {
                        tlFragmentGoodsMainGoodsSelectTitle3.getTabAt(finalIndexCid3).select();
                    }
                    tlFragmentGoodsMainGoodsSelectTitle3.addOnTabSelectedListener(onTabSelectedListener3);
                }, 100);
                if (tabItemSelectedResultListener != null) {
                    tabItemSelectedResultListener.onThirdCategorizeCallBack(thirdMenuEntity, finalIndexCid3);
                }
            } else {
                tlFragmentGoodsMainGoodsSelectTitle3.removeAllTabs();
            }

        }
    }

    /**
     * 重置所有属性值
     */
    private void resetAllValue() {
        this.firstMenuEntity = null;
        this.secondMenuEntity = null;
        this.thirdMenuEntity = null;
        this.tlFragmentGoodsMainGoodsSelectTitle1.removeAllTabs();
        this.tlFragmentGoodsMainGoodsSelectTitle1.setVisibility(View.GONE);
        this.ivFragmentGoodsMainGoodsTag1.setVisibility(View.GONE);
        this.tlFragmentGoodsMainGoodsSelectTitle2.removeAllTabs();
        this.tlFragmentGoodsMainGoodsSelectTitle2.setVisibility(View.GONE);
        this.ivFragmentGoodsMainGoodsTag2.setVisibility(View.GONE);
        this.tlFragmentGoodsMainGoodsSelectTitle3.removeAllTabs();
        this.tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.GONE);
        this.ivFragmentGoodsMainGoodsTag3.setVisibility(View.GONE);
        this.indexCid1 = 0;
        this.indexCid2 = 0;
    }

    /**
     * 对外提供接口回调，用于返回分类筛选结果
     *
     * @param itemSelectedResult TabItemSelectedResult 接口对象
     */
    public void addOnTabSelectedListener(TabItemSelectedResultListener itemSelectedResult) {
        this.tabItemSelectedResultListener = itemSelectedResult;
    }

    /**
     * 三级分类筛选监听器
     */
    public interface TabItemSelectedResultListener {
        /**
         * 一级分类筛选结果回调
         *
         * @param firstLevel 第一级分类
         * @param position   选中下标
         */
        void onFirstCategorizeCallBack(@Nullable MenuEntity<MenuEntity<MenuEntity>> firstLevel, int position);

        /**
         * 二级分类筛选结果回调
         *
         * @param secondLevel 第二级分类
         * @param position    选中下标
         */
        void onSecondCategorizeCallBack(@Nullable MenuEntity<MenuEntity> secondLevel, int position);

        /**
         * 三级分类筛选结果回调
         *
         * @param thirdLevel 第三级分类
         * @param position   选中下标
         */
        void onThirdCategorizeCallBack(@Nullable MenuEntity thirdLevel, int position);

        /**
         * 搜索按钮的点击事件
         */
        void onSearchClickListener();
    }


    /**
     * 获取二三级分裂
     */
    private void getClassListeData(String id, boolean issecondClass) {


        NewAPIManager.getInstance().getGoodsAPI().findTwoOrThreeLevelData(mAreaCode, id).enqueue(new Callback<BaseEntity<List<TwoOrThreeLevelEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TwoOrThreeLevelEntity>>> call, Response<BaseEntity<List<TwoOrThreeLevelEntity>>> response) {
                if (response.body() == null) {
                    return;
                }
                int indexCid3 = 0;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (issecondClass) {
                        secondMenuEntity_n.clear();
                    } else {
                        thirdMenuEntity_n.clear();
                    }
                    if (response.body().getData().size() == 0) {
                        return;
                    }

                    for (int i = 0; i < response.body().getData().size(); i++) {
                        MenuEntity data = new MenuEntity();
                        data.setUrl(response.body().getData().get(i).getImg());
                        data.setId(response.body().getData().get(i).getId());
                        data.setName(response.body().getData().get(i).getName());
                        if (issecondClass) {
                            if(secondCategory!=null&&secondCategory.equals(data.getId())){
                                indexCid2 = i;
                                secondCategory = null;//定位只定一次
                            }
                            secondMenuEntity_n.add(data);
                        } else {
                            if(thirdCategory!=null&&thirdCategory.equals(data.getId())){
                                indexCid3 = i;
                                thirdCategory = null;////定位只定一次
                            }
                            thirdMenuEntity_n.add(data);
                        }
                    }


                    if (issecondClass) {
                        if (indexCid1 == 0 || secondMenuEntity_n.size() == 0) {
                            tlFragmentGoodsMainGoodsSelectTitle2.setVisibility(View.GONE);
                            ivFragmentGoodsMainGoodsTag2.setVisibility(View.GONE);
                        } else {
                            tlFragmentGoodsMainGoodsSelectTitle2.setVisibility(View.VISIBLE);
                            ivFragmentGoodsMainGoodsTag2.setVisibility(View.VISIBLE);
                        }
                        tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.GONE);
                        ivFragmentGoodsMainGoodsTag3.setVisibility(View.GONE);
                        tlFragmentGoodsMainGoodsSelectTitle2.removeAllTabs();
                        for (int i = 0; i < secondMenuEntity_n.size(); i++) {
                            tlFragmentGoodsMainGoodsSelectTitle2.addTab(tlFragmentGoodsMainGoodsSelectTitle2.newTab().setText(secondMenuEntity_n.get(i).getName()), i==indexCid2);
                        }
                        tlFragmentGoodsMainGoodsSelectTitle2.removeOnTabSelectedListener(onTabSelectedListener2);
                        tlFragmentGoodsMainGoodsSelectTitle2.postDelayed(() -> {
                            if (tlFragmentGoodsMainGoodsSelectTitle2.getTabCount() > indexCid2 && tlFragmentGoodsMainGoodsSelectTitle2.getTabAt(indexCid2) != null) {
                                tlFragmentGoodsMainGoodsSelectTitle2.getTabAt(indexCid2).select();
                            }
                            tlFragmentGoodsMainGoodsSelectTitle2.addOnTabSelectedListener(onTabSelectedListener2);
                        }, 100);
                        if (tabItemSelectedResultListener != null) {
                            tabItemSelectedResultListener.onFirstCategorizeCallBack(firstMenuEntity, indexCid1);
                        }

                        firstMenuEntity.setDatas(secondMenuEntity_n);
                        if (tabItemSelectedResultListener != null) {
                            tabItemSelectedResultListener.onFirstCategorizeCallBack(firstMenuEntity, indexCid1);
                        }
                    } else {

                        if (indexCid2 == 0 || thirdMenuEntity_n.size() == 0) {
                            tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.GONE);
                            ivFragmentGoodsMainGoodsTag3.setVisibility(View.GONE);
                        } else {
                            tlFragmentGoodsMainGoodsSelectTitle3.setVisibility(View.VISIBLE);
                            ivFragmentGoodsMainGoodsTag3.setVisibility(View.VISIBLE);
                        }
//                        refreshTabTitle(indexCid1, indexCid2);
                        tlFragmentGoodsMainGoodsSelectTitle3.removeAllTabs();
                        for (MenuEntity children2 : thirdMenuEntity_n) {
                            tlFragmentGoodsMainGoodsSelectTitle3.addTab(tlFragmentGoodsMainGoodsSelectTitle3.newTab().setText(children2.getName()), false);
                        }
                        tlFragmentGoodsMainGoodsSelectTitle3.removeOnTabSelectedListener(onTabSelectedListener3);
                        int finalIndexCid = indexCid3;

                        tlFragmentGoodsMainGoodsSelectTitle3.postDelayed(() -> {
                            if (tlFragmentGoodsMainGoodsSelectTitle3.getTabCount() > 0 && tlFragmentGoodsMainGoodsSelectTitle3.getTabAt(finalIndexCid) != null) {
                                tlFragmentGoodsMainGoodsSelectTitle3.getTabAt(finalIndexCid).select();
                            }
                            tlFragmentGoodsMainGoodsSelectTitle3.addOnTabSelectedListener(onTabSelectedListener3);
                        }, 100);

                        secondMenuEntity.setDatas(thirdMenuEntity_n);
                        if (tabItemSelectedResultListener != null) {
                            tabItemSelectedResultListener.onSecondCategorizeCallBack(secondMenuEntity, indexCid2);
                        }
                        if(finalIndexCid>0){
                            thirdMenuEntity = thirdMenuEntity_n.get(finalIndexCid);
                            if (tabItemSelectedResultListener != null) {
                                tabItemSelectedResultListener.onThirdCategorizeCallBack(thirdMenuEntity, finalIndexCid);
                            }
                        }
                    }

                } else {
                    MToast.showToast(getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TwoOrThreeLevelEntity>>> call, Throwable t) {
            }
        });

    }


}
